
/**
* @type {import('@/types/global').handleSum}
*/
const handleSum = (param, needSumFieldList, sumText = "总价", offset = 1) => {

  if (needSumFieldList.length == 0) return []
  const { columns, data } = param;
  const sums = [];

  columns.forEach((column, index) => {

    if (index < offset) {
      sums[index] = '';
      return
    }
    if (index == offset) {
      sums[index] = sumText;
      return
    }

    if (needSumFieldList.includes(column.property)) {
      const sum = data
        .map((item) => Number(item[column.property]))
        .reduce((a, b) => a + b, 0);
      sums[index] = sum || '';
    } else {
      sums[index] = null;
    }
  });
  return sums;
}


export default handleSum
